package by.gusto.afisha.model;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import by.gusto.afisha.db.HelperFactory;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.field.DataType;
import com.j256.ormlite.field.DatabaseField;

public class Place {

	public static final String NAME_FIELD = "name";
	public static final String CATEGORY_ID_FIELD = "category_id";
	
	@DatabaseField(generatedId = true)
	public transient int id;
	
	@DatabaseField(dataType = DataType.STRING, columnName = NAME_FIELD)
	public String name;
	
	@DatabaseField(dataType = DataType.INTEGER, columnName = CATEGORY_ID_FIELD)
	public int categoryId;
	
	public Place(){}
	
	public Place(String name, int parentId) {
		this.name = name;
		categoryId = parentId;
	}

	public static ArrayList<Place> getPlacesByParentId(int parentId) {
		List<Place> places = null;
		try {
			Dao<Place, String> placeDao = HelperFactory.GetHelper().getPlaceDAO();
			places = placeDao.queryBuilder().where().eq(CATEGORY_ID_FIELD, parentId).query();
		} catch (SQLException ex) {
			ex.printStackTrace();
		}
		if(places == null)
			places = new ArrayList<Place>();
		return (ArrayList<Place>) places;
	}
}
